/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 */
package com.apache.order.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.NoSuchMessageException;

import com.apache.cache.util.Validator;
import com.apache.database.constant.SpringContextLoader;
import com.apache.tools.StrUtil;
import com.apache.tools.ToolsMap;

/**
 * description:  
 * @author Hou Dayu 创建时间：2015-9-23  
 */
public class MessageUtil {

	private static final String setMethodModify = "set";
	private static final char SEPARATOR = '_';

	public static String getMessage(String key, String[] params, String defaultMessage) {
		String msg = "";
		try {
			msg = SpringContextLoader.getContext().getMessage(key, params, Locale.CHINA);
			msg = Validator.getDefaultStr(msg, defaultMessage);
		} catch (NoSuchMessageException e) {
			msg = defaultMessage;
		}
		return Validator.getDefaultStr(msg, defaultMessage);
	}

	/**
	 * description:  Map转成JavaBean
	 * @param map
	 * @param obj
	 * @return   
	 */
	@SuppressWarnings("rawtypes")
	public static Object convert2Bean(Map<String, Object> map, Object obj) {
		Class class1 = obj.getClass();
		Method[] methods = class1.getMethods();
		ToolsMap<Object> utrMap = new ToolsMap<Object>();
		utrMap.putAll(map);
		for (Method method : methods) {
			String methodName = method.getName();
			try {
				if (methodName.startsWith(setMethodModify)) {
					String propertyName = methodName.substring(3);
					Object value = utrMap.get(propertyName);
					if (!Validator.isEmpty(value)) {
						Class<?>[] parameterTypes = method.getParameterTypes();
						String parameterName = parameterTypes[0].getName();
						if (parameterName.indexOf("Integer") != -1) {
							method.invoke(obj, Integer.valueOf(value.toString()));
						} else if (parameterName.indexOf("Long") != -1) {
							method.invoke(obj, Long.valueOf(value.toString()));
						} else {
							method.invoke(obj, String.valueOf(value));
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return obj;
	}

	/** 
	 * 对象转Map 
	 *  
	 * @param object  目标对象 
	 * @return 
	 * @throws IllegalAccessException 
	 * @throws InvocationTargetException 
	 * @throws NoSuchMethodException 
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> toMap(Object object) throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {
		return BeanUtils.describe(object);
	}

	/** 
	 * 对象转Map 
	 *  
	 * @param object  目标对象 
	 * @return 
	 * @throws IllegalAccessException 
	 * @throws InvocationTargetException 
	 * @throws NoSuchMethodException 
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> toMapDelNull(Object object) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<String, Object> map = BeanUtils.describe(object);
			for (String key : map.keySet()) {
				if ("class".equals(key))
					continue;
				if (!StrUtil.isEmpty(map.get(key)))
					result.put(key, map.get(key));
			}
		} catch (Exception e) {

		}
		return result;
	}

	/** 
	 * 转换成Map并提供字段命名驼峰转平行 
	 *  
	 * @param clazz  目标对象所在类 
	 * @param object  目标对象 
	 * @param map  待转换Map 
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public static Map<String, String> toMapForFlat(Object object) throws IllegalAccessException,
			InvocationTargetException, NoSuchMethodException {
		Map<String, String> map = toMap(object);
		return toUnderlineStringMap(map);
	}

	/** 
	 * 将Map的Keys转译成下划线格式的<br> 
	 * (例:branchNo -> branch_no)<br> 
	 *  
	 * @param map   待转换Map 
	 * @return 
	 */
	public static <V> Map<String, V> toUnderlineStringMap(Map<String, V> map) {
		Map<String, V> newMap = new HashMap<String, V>();
		for (String key : map.keySet()) {
			newMap.put(toUnderlineString(key), map.get(key));
		}
		return newMap;
	}

	/** 
	 * 将属性样式字符串转成驼峰样式字符串<br> 
	 * (例:branchNo -> branch_no)<br> 
	 *  
	 * @param inputString 
	 * @return 
	 */
	public static String toUnderlineString(String inputString) {
		if (inputString == null)
			return null;
		StringBuilder sb = new StringBuilder();
		boolean upperCase = false;
		for (int i = 0; i < inputString.length(); i++) {
			char c = inputString.charAt(i);
			boolean nextUpperCase = true;
			if (i < (inputString.length() - 1)) {
				nextUpperCase = Character.isUpperCase(inputString.charAt(i + 1));
			}
			if ((i >= 0) && Character.isUpperCase(c)) {
				if (!upperCase || !nextUpperCase) {
					if (i > 0)
						sb.append(SEPARATOR);
				}
				upperCase = true;
			} else {
				upperCase = false;
			}
			sb.append(Character.toLowerCase(c));
		}
		return sb.toString();
	}
}
